package com.tree.thrive.global.utils;

import com.tree.thrive.business.input.domain.bo.InputCheckBO;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

/**
 * top-tree-out-of-window
 *
 * @Auther: cheng.tang
 * @Date: 2022/4/27 7:23 PM
 * @Description:
 */
public class LambdaGroupBy {


    @Test
    public void testGroupByCount() {
        List<InputCheckBO> msgList = new ArrayList<>();
        String peopleA = "PeopleA";
        long peopleACount = 2L;
        addBO(msgList, peopleACount, peopleA);
        String peopleB = "PeopleB";
        long peopleBCount = 10L;
        addBO(msgList, peopleBCount, peopleB);
        Map<String, Long> map = msgList.stream().collect(Collectors.groupingBy(InputCheckBO::getName, Collectors.counting()));
        assertThat(map.get(peopleA)).isEqualTo(peopleACount);
        assertThat(map.get(peopleB)).isEqualTo(peopleBCount);
    }


    private void addBO(List<InputCheckBO> msgList, Long count, String name) {
        for (int i = 0; i < count; i++) {
            InputCheckBO inputCheckBO = new InputCheckBO();
            inputCheckBO.setName(name);
            msgList.add(inputCheckBO);
        }
    }


    @Test
    public void testMapIf() {
        Map<String, List<String>> sku2List = new HashMap<>();
        sku2List.computeIfAbsent("A", k -> new ArrayList<>());
        List<String> skuList = sku2List.get("A");
        assertThat(skuList).isNotNull();
        assertThat(skuList.size()).isEqualTo(0);
        skuList.add("B");
        List<String> resultList = sku2List.computeIfAbsent("A", k -> new ArrayList<>());
        assertThat(resultList).isNotNull();
        assertThat(resultList.size()).isEqualTo(1);


        List<String> result = Collections.singletonList("1");
        List<String> test = new ArrayList<>();
        test.addAll(result);
        System.out.println(test);
    }


}
